package itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import itheima.Mapper.DishMapper;
import itheima.common.CustomExcrption;
import itheima.dto.DishDto;
import itheima.entity.Category;
import itheima.entity.Dish;
import itheima.entity.DishFlavor;
import itheima.service.DishFlavorService;
import itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2022-09-16-18:31
 */
@Service
@Slf4j
public class DishServicelmpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dto
     */

    @Transactional//因为有多张表，所以开启事物注解,确保一致性
    public void saveWithFlavor(DishDto dto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dto);

        Long dishId = dto.getId();//菜品id

        //菜品口味
        List<DishFlavor> flavors = dto.getFlavors();
        //stream流遍历，值为item
       flavors = flavors.stream().map((item) ->{
           //设置id：因为没有封住上dishId
            item.setDishId(dishId);
            return item;
            //重新转成List，然后赋值给flavors
        }).collect(Collectors.toList());

       dishFlavorService.saveBatch(flavors);
        }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */

    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //查询菜品基本信息,从dish表中查询
        Dish dish = this.getById(id);

        //查询到菜品的信息拷贝给dishDao
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //因为一个菜品中有多种口味，所以要用集合
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

        //设置口味
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);
        //添加当前提交过来的口味数据--dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{

            item.setDishId(dishDto.getId());
            return item;
            //重新转成List，然后赋值给flavors
        }).collect(Collectors.toList());
        //saveBatch:批量保存
        dishFlavorService.saveBatch(flavors);

    }


    @Transactional
    public void removeWithFlavor(List<Long> ids) {

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);
        lambdaQueryWrapper.eq(Dish::getStatus,1);


        int count = this.count(lambdaQueryWrapper);
        if (count >0){
            throw new CustomExcrption("菜品正在售卖，不能删除");

        }

        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.in(DishFlavor::getDishId,ids);


        dishFlavorService.remove(lqw);








    }

}

